﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using AussieHouseSitters.Data;
using Calendar = AussieHouseSitters.Data.Calendar;

namespace AussieHouseSitters.Controllers
{
    [Authorize]
    public class CalendarController : Controller
    {
        private readonly IContextWrapper _contextWrapper = new UserHttpContext();
        private readonly IDataService _dataService = new DataService();

        //
        // GET: /Calendar/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Items()
        {
            var user = _contextWrapper.GetUser();
            var userCals = _dataService.GetCalendarForUser(user.Identity);
            return View(userCals);
        }
    }

    public interface IDataService
    {
        IEnumerable<UserCalendarItem> GetCalendarForUser(IIdentity identity);
    }

    public class UserCalendarItem
    {
        public int Month { get; set; }
        public int Period { get; set; }

        public string MonthName
        {
            get { return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month); }
        }

        public int Year { get; set; }

        public string PreferredLocation { get; set; }
    }

    public class DataService : IDataService
    {
        public IEnumerable<UserCalendarItem> GetCalendarForUser(IIdentity identity)
        {
            using (var db = GetDbContext())
            {
                return db.Calendars
                         .Where(c => c.HouseSitter.aspnet_Users.UserName == identity.Name)
                         .Include("CalendarRange")
                         .Select(CreateCalendarItem)
                         .ToList();
            }
        }

        private UserCalendarItem CreateCalendarItem(Calendar calendarItem)
        {
            var monthNo = calendarItem.CalendarRange.DateFrom.Month;
            var firstWeek = 7 - (int)new DateTime(calendarItem.CalendarRange.DateFrom.Year, monthNo, 1).DayOfWeek;
            var weekNo = (calendarItem.CalendarRange.DateFrom.Day - firstWeek) / 7 + 1;
            return new UserCalendarItem
                {
                    Year = calendarItem.CalendarRange.DateFrom.Year,
                    Month = monthNo,
                    Period = weekNo,
                    PreferredLocation = calendarItem.PreferredLocation
                };
        }

        private AuusieHouseSittersEntities GetDbContext()
        {
            return new AuusieHouseSittersEntities();
        }
    }

    internal interface IContextWrapper
    {
        IPrincipal GetUser();
    }

    internal class UserHttpContext : IContextWrapper
    {
        public IPrincipal GetUser()
        {
            return HttpContext.Current.User;
        }
    }
}
